<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sa-Token 权限测试页面</title>
    <script src="jquery/jquery3.6.js"></script>
    <script src="URLJS/URL.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            border-bottom: 2px solid #4CAF50;
            padding-bottom: 10px;
        }
        .section {
            margin: 20px 0;
            padding: 15px;
            background: #f9f9f9;
            border-left: 4px solid #4CAF50;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #45a049;
        }
        button.danger {
            background-color: #f44336;
        }
        button.danger:hover {
            background-color: #da190b;
        }
        .info {
            background-color: #e7f3fe;
            border-left: 4px solid #2196F3;
            padding: 10px;
            margin: 10px 0;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .success {
            color: #4CAF50;
        }
        .error {
            color: #f44336;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Sa-Token 权限测试页面</h1>

        <div class="info">
            <strong>当前 Token:</strong> <span id="currentToken">未登录</span><br>
            <strong>登录状态:</strong> <span id="loginStatus">未知</span>
        </div>

        <div class="section">
            <h3>1. 公开接口测试（无需登录）</h3>
            <button onclick="testPublicApi()">访问公开接口</button>
            <div id="publicResult" class="result"></div>
        </div>

        <div class="section">
            <h3>2. 私有接口测试（需要登录）</h3>
            <button onclick="testPrivateApi()">访问私有接口</button>
            <div id="privateResult" class="result"></div>
        </div>

        <div class="section">
            <h3>3. 获取用户信息</h3>
            <button onclick="getUserInfo()">获取当前用户信息</button>
            <div id="userInfoResult" class="result"></div>
        </div>

        <div class="section">
            <h3>4. 检查登录状态</h3>
            <button onclick="checkLogin()">检查登录状态</button>
            <div id="checkLoginResult" class="result"></div>
        </div>

        <div class="section">
            <h3>5. 登出测试</h3>
            <button class="danger" onclick="logout()">登出</button>
            <div id="logoutResult" class="result"></div>
        </div>

        <div class="section">
            <h3>6. 跳转到登录页</h3>
            <button onclick="goToLogin()">前往登录</button>
        </div>
    </div>

    <script>
        // 页面加载时显示当前 token
        $(document).ready(function() {
            updateTokenDisplay();
        });

        function updateTokenDisplay() {
            var token = localStorage.getItem('token');
            $('#currentToken').text(token || '未登录');
            $('#loginStatus').text(token ? '已登录' : '未登录')
                .css('color', token ? '#4CAF50' : '#f44336');
        }

        // 测试公开接口
        function testPublicApi() {
            $.ajax({
                url: HTTPURL + '/auth/public/hello',
                method: 'GET',
                success: function(response) {
                    $('#publicResult').html('<span class="success">✓ 成功</span>\n' + JSON.stringify(response, null, 2));
                },
                error: function(xhr, status, error) {
                    $('#publicResult').html('<span class="error">✗ 失败</span>\n' +
                        'Status: ' + xhr.status + '\n' +
                        'Error: ' + error);
                }
            });
        }

        // 测试私有接口
        function testPrivateApi() {
            var token = localStorage.getItem('token');
            $.ajax({
                url: HTTPURL + '/auth/private/hello',
                method: 'GET',
                headers: {
                    'token': token
                },
                success: function(response) {
                    $('#privateResult').html('<span class="success">✓ 成功</span>\n' + JSON.stringify(response, null, 2));
                },
                error: function(xhr, status, error) {
                    $('#privateResult').html('<span class="error">✗ 失败</span>\n' +
                        'Status: ' + xhr.status + '\n' +
                        'Error: ' + (xhr.responseJSON ? JSON.stringify(xhr.responseJSON, null, 2) : error));
                }
            });
        }

        // 获取用户信息
        function getUserInfo() {
            var token = localStorage.getItem('token');
            $.ajax({
                url: HTTPURL + '/auth/userInfo',
                method: 'GET',
                headers: {
                    'token': token
                },
                success: function(response) {
                    $('#userInfoResult').html('<span class="success">✓ 成功</span>\n' + JSON.stringify(response, null, 2));
                },
                error: function(xhr, status, error) {
                    $('#userInfoResult').html('<span class="error">✗ 失败</span>\n' +
                        'Status: ' + xhr.status + '\n' +
                        'Error: ' + (xhr.responseJSON ? JSON.stringify(xhr.responseJSON, null, 2) : error));
                }
            });
        }

        // 检查登录状态
        function checkLogin() {
            $.ajax({
                url: HTTPURL + '/auth/checkLogin',
                method: 'GET',
                success: function(response) {
                    $('#checkLoginResult').html('<span class="success">✓ 成功</span>\n' + JSON.stringify(response, null, 2));
                    updateTokenDisplay();
                },
                error: function(xhr, status, error) {
                    $('#checkLoginResult').html('<span class="error">✗ 失败</span>\n' +
                        'Status: ' + xhr.status + '\n' +
                        'Error: ' + error);
                }
            });
        }

        // 登出
        function logout() {
            var token = localStorage.getItem('token');
            if (!token) {
                $('#logoutResult').html('<span class="error">✗ 未登录</span>');
                return;
            }

            $.ajax({
                url: HTTPURL + '/Logout',
                method: 'POST',
                headers: {
                    'token': token
                },
                success: function(response) {
                    localStorage.removeItem('token');
                    localStorage.removeItem('username');
                    $('#logoutResult').html('<span class="success">✓ 登出成功</span>\n' + JSON.stringify(response, null, 2));
                    updateTokenDisplay();
                },
                error: function(xhr, status, error) {
                    $('#logoutResult').html('<span class="error">✗ 登出失败</span>\n' +
                        'Status: ' + xhr.status + '\n' +
                        'Error: ' + error);
                }
            });
        }

        // 跳转到登录页
        function goToLogin() {
            window.location.href = '/aaaa/login/code/115/115.html';
        }
    </script>
</body>
</html>
